"""添加角色表和用户角色关联表

Revision ID: d40f51b02790
Revises: 86bfb4ea95ab
Create Date: 2025-11-20 09:53:18.529782

"""
from typing import Sequence, Union

import sqlalchemy as sa
import sqlmodel
from alembic import op

# revision identifiers, used by Alembic.
revision: str = 'd40f51b02790'
down_revision: Union[str, Sequence[str], None] = '86bfb4ea95ab'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('roles',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('create_time', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
    sa.Column('update_time', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
    sa.Column('role_no', sqlmodel.sql.sqltypes.AutoString(length=10), nullable=False, comment='编号'),
    sa.Column('role_name', sqlmodel.sql.sqltypes.AutoString(length=50), nullable=False, comment='名称'),
    sa.Column('role_desc', sqlmodel.sql.sqltypes.AutoString(length=200), nullable=False, comment='描述'),
    sa.Column('status', sa.Enum('Y', 'N', name='rolestatusenum'), server_default='Y', nullable=True, comment='状态: Y-激活, N-禁用'),
    sa.PrimaryKeyConstraint('id'),
    comment='角色表'
    )
    op.create_index(op.f('ix_roles_role_name'), 'roles', ['role_name'], unique=False)
    op.create_index(op.f('ix_roles_role_no'), 'roles', ['role_no'], unique=True)
    op.create_table('userrolelink',
    sa.Column('user_id', sa.Integer(), nullable=False, comment='用户ID'),
    sa.Column('role_id', sa.Integer(), nullable=False, comment='角色ID'),
    sa.ForeignKeyConstraint(['role_id'], ['roles.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('user_id', 'role_id'),
    comment='用户角色关联表'
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('userrolelink')
    op.drop_index(op.f('ix_roles_role_no'), table_name='roles')
    op.drop_index(op.f('ix_roles_role_name'), table_name='roles')
    op.drop_table('roles')
    # ### end Alembic commands ###
